<?php
/**
 *   OPENSHOP     Supply Chain Management,SCM
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: depot_seat.php,v 1.0 2009/07/13 09:36:58 created $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);
require(dirname(__FILE__) . '/includes/init.php');

$exc_depot 			= 	new exchange($os->table('depot'), $db, 'depot_id', 'depot_code');
$exc_seat 			= 	new exchange($os->table('seat'), $db, 'seat_id', 'seat_code');
$depot_types        =   array( 1=>'默认类型', 2 =>'清单类型');

$action 			= 	isset($_REQUEST['act']) ? $_REQUEST['act'] : 'list_check_order';

if($_REQUEST['act']=='list_depot')
{
	//检查权限
	admin_priv();
		
    $smarty->assign('ur_here',      '仓库列表');
    $smarty->assign('action_link',  array('text' => '添加仓库', 'href' => 'depot_seat.php?act=add_depot'));
    $smarty->assign('full_page',    1);

    $depot_list = get_depotlist();

    $smarty->assign('depot_list',   $depot_list['depot']);
    $smarty->assign('filter',       $depot_list['filter']);
    $smarty->assign('record_count', $depot_list['record_count']);
    $smarty->assign('page_count',   $depot_list['page_count']);
    
    assign_query_info();
    
    $smarty->display('depot_list.tpl');	
}

/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query_depot')
{
    $list = get_depotlist();
    $smarty->assign('depot_list',     $list['depot']);
    $smarty->assign('filter',         $list['filter']);
    $smarty->assign('record_count',   $list['record_count']);
    $smarty->assign('page_count',     $list['page_count']);
    
    make_json_result($smarty->fetch('depot_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

elseif ($_REQUEST['act'] == 'add_depot' || $_REQUEST['act'] == 'edit_depot')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add_depot';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_depot' : 'edit_depot';
    admin_priv($priv_str);
    
    $smarty->assign('form_action', $is_add ? 'insert_depot' : 'update_depot');

    /* 初始化、取得仓库信息 */
    if ($is_add)
    {
        $depot = array(
            'depot_id'     		    => 0,
            'depot_name'   		    => '',
            'depot_code'   		    => '', 
            'depot_type'   		    => -1,
            'is_show'      		    => 1
        );
        
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }

	    $depot_id = intval($_GET['id']);
	    $depot = get_depot_info($depot_id);
	    if (empty($depot))
	    {
	        sys_msg('仓库不存在');
	    }
    }
    
    $depot['depot_types']   =  $depot_types; 
    $smarty->assign('depot', $depot);
    
    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加仓库');
    }
    else
    {
        $smarty->assign('ur_here', '编辑仓库');
    }
    
    if ($is_add)
    {
        $href = 'depot_seat.php?act=list_depot';
    }
    else
    {
        $href = 'depot_seat.php?act=list_depot&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '仓库列表'));
    
    assign_query_info();
    
    $smarty->display('depot_info.tpl');     
}

elseif ($_REQUEST['act'] == 'insert_depot' || $_REQUEST['act'] == 'update_depot')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert_depot';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_depot' : 'edit_depot';
    admin_priv($priv_str);

    /* 提交值 */
    $depot = array(
        'depot_id'     			    => intval($_POST['id']),
        'depot_name'   			    => sub_str($_POST['depot_name'], 100, false),    
		'depot_code'   			    => sub_str($_POST['depot_code'], 80, false),
		'depot_type'      			=> !empty($_POST['depot_type']) ? intval($_POST['depot_type']) : 0,
		'is_show'   			    => !empty($_POST['is_show']) ? intval($_POST['is_show']) : 0
    );
    
    /* 判断名称是否重复 */
    if (!$exc_depot->is_only('depot_name', $depot['depot_name'], $depot['depot_id']))
    {
        sys_msg('该仓库名称已存在，请您换一个名称');
    }
    
    if (!$exc_depot->is_only('depot_code', $depot['depot_code'], $depot['depot_id']))
    {
        sys_msg('该仓库代码已存在，请您换一个代码');
    }
    /* 保存供应商类型信息 */
    if ($is_add)
    {
    	$depot['add_time'] = gmtime();
        $db->autoExecute($os->table('depot'), $depot, 'INSERT');
        $depot['depot_id'] = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('depot'), $depot, 'UPDATE', "depot_id = '$depot[depot_id]'");
    }
   
    /* 记日志 */
    if ($is_add)
    {
        admin_log($depot['depot_name'], 'add', 'depot');
    }
    else
    {
        admin_log($depot['depot_name'], 'edit', 'depot');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'depot_seat.php?act=add_depot', 'text' => '继续添加新的仓库'),
            array('href' => 'depot_seat.php?act=list_depot', 'text' => '返回仓库列表')
        );
        sys_msg('添加仓库成功', 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'depot_seat.php?act=list_depot&' . list_link_postfix(), 'text' => '返回仓库列表')
        );
        sys_msg('编辑仓库成功', 0, $links);
    }	
}

elseif($_REQUEST['act']=='remove_depot'){
    check_authz_json('drop_depot');

    $depot_id = intval($_GET['id']);
    $depot = get_depot_info($depot_id);
    if (empty($depot))
    {
        sys_msg('仓库不存在');
    }

    if($db->query("DELETE FROM " . $os->table('depot') . " WHERE depot_id='$depot_id'")){
	    /* 记日志 */
	    admin_log(sprintf('仓库 s%', $depot['depot_name']), 'remove', 'depot');	
	    $db->query("DELETE FROM " . $os->table('seat') . " WHERE depot_id='$depot_id'");
    }
    
    /* 清除缓存 */
    clear_cache_files();    
    
    $url = 'depot_seat.php?act=query_depot&' . str_replace('act=remove_depot', '', $_SERVER['QUERY_STRING']);
    os_header("Location: $url\n");
    exit;		
}

elseif($_REQUEST['act']=='list_seat'){
	
	//检查权限
	admin_priv();

    $depot_id = intval($_GET['depot_id']);
    $depot    = get_depot_info($depot_id);
    if (empty($depot))
    {
        sys_msg('仓库不存在');
    }
    
    $smarty->assign('ur_here',      '库位列表');
    $smarty->assign('action_link',  array('text' => '添加库位', 'href' => 'depot_seat.php?act=add_seat&depot_id='.$depot_id));
    $smarty->assign('action_link2',  array('text' => '仓库列表', 'href' => 'depot_seat.php?act=list_depot'));
    $smarty->assign('full_page',    1);

    $seat_list = get_seatlist();

    $smarty->assign('seat_list',    $seat_list['seat']);
    $smarty->assign('filter',       $seat_list['filter']);
    $smarty->assign('record_count', $seat_list['record_count']);
    $smarty->assign('page_count',   $seat_list['page_count']);
    
    assign_query_info();
        
    $smarty->display('seat_list.tpl');		
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'query_seat')
{
    $list = get_seatlist();
    $smarty->assign('seat_list',      $list['seat']);
    $smarty->assign('filter',         $list['filter']);
    $smarty->assign('record_count',   $list['record_count']);
    $smarty->assign('page_count',     $list['page_count']);
    
    make_json_result($smarty->fetch('seat_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

elseif ($_REQUEST['act'] == 'add_seat' || $_REQUEST['act'] == 'edit_seat')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'add_seat';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_seat' : 'edit_seat';
    admin_priv($priv_str);
    
    $smarty->assign('form_action', $is_add ? 'insert_seat' : 'update_seat');

    /* 初始化、取得仓库信息 */
    if ($is_add)
    {
        $seat = array(
            'seat_id'     		    => 0,
            'seat_name'   		    => '',
            'seat_code'   		    => '',
            'depot_id'              => intval($_GET['depot_id']),
            'is_show'      		    => 1
        );
        
    }
    else
    {
        if (empty($_GET['id']))
        {
            sys_msg('invalid param');
        }

        $id  = intval($_GET['id']);
        $sql = "SELECT * FROM " . $os->table('seat') . " WHERE seat_id='$id'";
        $seat = $db->getRow($sql);
        if (empty($seat))
        {
            sys_msg('库位不存在');
        }
    }
    
    $depot    = get_depot_info($seat['depot_id']);
    if (empty($depot))
    {
        sys_msg('仓库不存在');
    }
 
    $seat['depot_name']   		    = $depot['depot_name'];       
    $smarty->assign('seat', $seat);
    
    /* 显示模板 */
    if ($is_add)
    {
        $smarty->assign('ur_here', '添加库位');
    }
    else
    {
        $smarty->assign('ur_here', '编辑库位');
    }
    
    if ($is_add)
    {
        $href = 'depot_seat.php?act=list_seat&depot_id='.$seat['depot_id'];
    }
    else
    {
        $href = 'depot_seat.php?act=list_seat&depot_id='.$seat['depot_id'].'&' . list_link_postfix();
    }
    $smarty->assign('action_link', array('href' => $href, 'text' => '库位列表'));
    
    assign_query_info();   
     
    $smarty->display('seat_info.tpl');     
}

elseif ($_REQUEST['act'] == 'insert_seat' || $_REQUEST['act'] == 'update_seat')
{
    /* 是否添加 */
    $is_add = $_REQUEST['act'] == 'insert_seat';
    /* 权限检查 */
    $priv_str = $is_add ? 'add_seat' : 'edit_seat';
    admin_priv($priv_str);

    $depot_id = intval($_POST['depot_id']);
    $depot    = get_depot_info($depot_id);
    if (empty($depot))
    {
        sys_msg('仓库不存在');
    }
    
    /* 提交值 */
    $seat = array(
        'seat_id'     			    => intval($_POST['id']),
        'depot_id'                  => $depot_id,
        'seat_name'   			    => sub_str($_POST['seat_name'], 100, false),    
		'seat_code'   			    => sub_str($_POST['seat_code'], 80, false),
		'is_show'   			    => !empty($_POST['is_show']) ? intval($_POST['is_show']) : 0
    );
    
    /* 判断名称是否重复 */
    if (!$exc_seat->is_only('seat_name', $seat['seat_name'], $seat['seat_id']))
    {
        sys_msg('该库位名称已存在，请您换一个名称');
    }
    
    if (!$exc_seat->is_only('seat_code', $seat['seat_code'], $seat['seat_id']))
    {
        sys_msg('该库位代码已存在，请您换一个代码');
    }
    /* 保存供应商类型信息 */
    if ($is_add)
    {
    	$seat['add_time'] = gmtime();
        $db->autoExecute($os->table('seat'), $seat, 'INSERT');
        $seat['seat_id']  = $db->insert_id();
    }
    else
    {
        $db->autoExecute($os->table('seat'), $seat, 'UPDATE', "seat_id = '$seat[seat_id]'");
    }
   
    /* 记日志 */
    if ($is_add)
    {
        admin_log($seat['seat_name'], 'add', 'seat');
    }
    else
    {
        admin_log($seat['seat_name'], 'edit', 'seat');
    }

    /* 清除缓存 */
    clear_cache_files();

    /* 提示信息 */
    if ($is_add)
    {
        $links = array(
            array('href' => 'depot_seat.php?act=add_seat&depot_id='.$depot_id, 'text' => '继续添加新的库位'),
            array('href' => 'depot_seat.php?act=list_seat&depot_id='.$depot_id, 'text' => '返回库位列表')
        );
        sys_msg('添加库位成功', 0, $links);
    }
    else
    {
        $links = array(
            array('href' => 'depot_seat.php?act=list_seat&depot_id='.$depot_id.'&' . list_link_postfix(), 'text' => '返回库位列表')
        );
        sys_msg('编辑库位成功', 0, $links);
    }	
}

elseif($_REQUEST['act']=='remove_seat'){
    check_authz_json('drop_seat');

    $seat_id = intval($_GET['id']);
    $sql = "SELECT * FROM " . $os->table('seat') . " WHERE seat_id='$seat_id'";
    $seat = $db->getRow($sql);
    if (empty($seat))
    {
        sys_msg('库位不存在');
    }

    if($db->query("DELETE FROM " . $os->table('seat') . " WHERE seat_id='$seat_id'")){
	    /* 记日志 */
	    admin_log(sprintf('库位 s%', $seat['seat_name']), 'remove', 'seat');	
    }
    
    /* 清除缓存 */
    clear_cache_files();    
    
    $url = 'depot_seat.php?act=query_seat&' . str_replace('act=remove_seat', '', $_SERVER['QUERY_STRING']);
    os_header("Location: $url\n");
    exit;		
}

function get_depot_info($depot_id){
    $sql = "SELECT * FROM " . $GLOBALS['os']->table('depot') . " WHERE depot_id=" . intval($depot_id);
    $depot = $GLOBALS['db']->getRow($sql);	
    return $depot;
}

function get_depotlist()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'depot_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }     
           
        $where = 'WHERE 1 ';
        if ($filter['keyword'])
        {
			$where .= " AND (depot_code LIKE '%" . mysql_like_quote($filter['keyword']) . "%' OR depot_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%')";
        }   
        /* 查询记录总数，计算分页数 */
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('depot') . $where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT * FROM " . $GLOBALS['os']->table('depot') . " $where ORDER BY $filter[sort_by] $filter[sort_order]";

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
    	$row['depot_type_name'] = $GLOBALS['depot_types'][$row['depot_type']];
        $arr[] = $row;
    }

    return array('depot' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

function get_seatlist(){
    $result = get_filter();
    if ($result === false)
    {
        /* 初始化分页参数 */
        $filter = array();
        $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'seat_id' : trim($_REQUEST['sort_by']);
        $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
        $filter['depot_id']   = empty($_REQUEST['depot_id']) ? 0 : intval($_REQUEST['depot_id']);
        $filter['keyword']    = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }     
           
        $where = "WHERE s.depot_id=$filter[depot_id]";
        if ($filter['keyword'])
        {
			$where .= " AND (s.seat_code LIKE '%" . mysql_like_quote($filter['keyword']) . "%' OR s.seat_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%')";
        }   
        /* 查询记录总数，计算分页数 */
        $sql = "SELECT COUNT(*) FROM " . $GLOBALS['os']->table('seat') . " s LEFT JOIN ". $GLOBALS['os']->table('depot') . 
               " d ON s.depot_id=d.depot_id $where";
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);
        $filter = page_and_size($filter);

        /* 查询记录 */
        $sql = "SELECT s.*,d.depot_name FROM " . $GLOBALS['os']->table('seat') . " s LEFT JOIN ". $GLOBALS['os']->table('depot') . 
               " d ON s.depot_id=d.depot_id $where ORDER BY $filter[sort_by] $filter[sort_order]";

        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);

    $arr = array();
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $row;
    }

    return array('seat' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}
?>